<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
  import echarts from 'echarts'
  import 'echarts/map/js/china.js'

  export default {
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '600px'
      },
      areaData: {
        type: Array
      }
    },
    data() {
      return {
        chart: null,
        type: []
      }
    },
    watch: {
      areaData: {
        deep: true,
        handler(val) {
          this.setOptions(val)
        }
      }
    },
    mounted() {
      this.initChart()
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      setOptions(areaData) {
        this.chartOption = {
          tooltip: {
            trigger: 'item',
            formatter: function(params) {
              return params.name + '<br>公共微客    ' + (!params.value ? 0 : params.value)
            }
          },
          visualMap: {
            min: 0,
            max: 10000,
            realtime: false,
            calculable: true,
            right: 10,
            padding: 0,
            inRange: {
              color: ['#FDEEB5', '#F8C608']
            }
          },
          geo: {
            map: 'china'
          },
          series: [
            {
              type: 'map',
              geoIndex: 0,
              label: {
                show: true
              },
              data: areaData
            }
          ]
        }
        this.chart.setOption(this.chartOption)
      },
      initChart() {
        this.chart = echarts.init(this.$el)
        this.setOptions(this.areaData)
      }
    }
  }
</script>